import 'package:bxultimate/app/router/router_name.dart';
import 'package:bxultimate/app/theme/app_theme.dart';
import 'package:bxultimate/app/theme/gaps.dart';
import 'package:bxultimate/data/store/config_store.dart';
import 'package:bxultimate/domain/application/organization/OrganizationAnnouncementAnalysisUserDto.dart';
import 'package:bxultimate/resources/assets.dart';
import 'package:bxultimate/widgets/mk_load_image.dart';
import 'package:bxultimate/widgets/mk_page.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import 'controller.dart';

class OrganizationStatisticsTaskAnalysisPage extends StatefulWidget {
  const OrganizationStatisticsTaskAnalysisPage({Key? key}) : super(key: key);

  @override
  State<OrganizationStatisticsTaskAnalysisPage> createState() => _OrganizationStatisticsTaskAnalysisPageState();
}

class _OrganizationStatisticsTaskAnalysisPageState extends State<OrganizationStatisticsTaskAnalysisPage> {
  final controller = Get.find<OrganizationStatisticsTaskAnalysisController>();
  final state = Get.find<OrganizationStatisticsTaskAnalysisController>().state;

  Widget _buildBody() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
          Container(
            width: double.infinity,
            padding: EdgeInsets.all(8),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(8),
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text('${state.taskInfo.taskTitle}',style: TextStyle(fontSize: 16,fontWeight: FontWeight.bold),),
                Gaps.vGap8,
                Text('${state.taskInfo.taskDescription}',style: TextStyle(color: Colors.grey),)
              ],
            ),
          ),
        Gaps.vGap8,
        controller.tabController == null ? SizedBox(): TDTabBar(
          tabs: state.tabs,
          controller: controller.tabController,
          showIndicator: true,
          // backgroundColor: Colors.white,
          isScrollable: false,),
        controller.tabController == null ? SizedBox(): Expanded(
          child: TabBarView(
            controller: controller.tabController,
            children: [
              _buildItem(state.readUserList),
              _buildItem(state.unreadUserList),
              _buildItem(state.finishUserList),
              _buildItem(state.unFinishUserList),
            ],
          ),
        )
      ]
    );
  }

  Widget _buildItem(List<OrganizationAnnouncementAnalysisUserDto> list) {
    return ListView.builder(
      itemCount: list.length,
      itemBuilder: (context, index) {
        return GestureDetector(
          onTap: (){
            // if(list[index].isFinish != -1){
              // 跳转详情
              Get.toNamed(RouterName.organizationStatisticsTaskDetail,arguments: list[index].id);
            // }
          },
          child: Container(
            margin: EdgeInsets.only(bottom: 4),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(8),
              color: Colors.white,
              border: Border(
                bottom: BorderSide(
                  color: Colors.grey.withOpacity(0.1),
                )
              )
            ),
            child: ListTile(
              leading: ClipRRect(
                borderRadius: BorderRadius.circular(8),
                child: MkLoadImage(
                  list[index].headImg ==null? Assets.imagesIcon: ConfigStore.to.baseFileUrl + list[index].headImg!,
                  width: 40,
                  height: 40,
                  fit: BoxFit.cover,
                ),
              ),
              title: Text('${list[index].name}'),
              subtitle: Text('${list[index].submitTime != null? '提交于${list[index].submitTime}':'未提交'}'),
            ),
          ),
        );
      }
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: OrganizationStatisticsTaskAnalysisController(),
        builder: (_)=>MkPage(child: Scaffold(
      backgroundColor: Colors.transparent,
      appBar: TDNavBar(
        backgroundColor: Colors.transparent,
        title: '任务统计',
        rightBarItems: [
          TDNavBarItem(
            action: controller.export,
            iconWidget: Icon(Icons.import_export),
          ),
        ],
      ),
      body: Padding(
        padding: EdgeInsets.only(left: AppTheme.iDefaultPadding,right: AppTheme.iDefaultPadding),
        child: _buildBody(),
      ),
    )));
  }

  @override
  void dispose() {
    Get.delete<OrganizationStatisticsTaskAnalysisController>();
    super.dispose();
  }
}